<table class="layui-hide" id="Show" lay-filter="Show"></table>
<div>
    <form class="layui-form"  action="">
        <div class="layui-form-item">
            <label class="layui-form-label">选择科目:</label>
            <div class="layui-input-block">
                <div>
                    <select name="subName" id="subName" lay-verify="">
                        <option value="Chinese">语文</option>
                        <option value="Math">数学</option>
                        <option value="English">英语</option>
                    </select>
                </div>
            </div>
        </div>


        <!-- <input id="confirm" type="button" class="layui-btn layui-btn-lg layui-btn-normal" style="margin-left: 1550px;" value="确认科目"></input> -->


        <!-- <div class="layui-form-item">
            <label class="layui-form-label">图片</label>
            <div class="layui-input-block">
                <div class="layui-upload">
                    <button type="button" class="layui-btn" id="test1">上传图片</button>
                    <input type="hidden" name="img" id="img"/>
                    <div class="layui-upload-list">
                    <img class="layui-upload-img" id="demo1">
                    <p id="demoText"></p>
                    </div>
                </div>  
            </div>
        </div> -->
        <div class="layui-form-item">
            <label class="layui-form-label">题干</label>
            <div class="layui-input-block">
                <textarea id="mytextarea" name="subMain" placeholder="请输入题目内容及选项" class="mytextarea"></textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">答案</label>
            <div class="layui-input-block">
                <input type="text" name="subAnswer" id="answer" lay-verify="title" autocomplete="off" placeholder="请输入答案"
                    class="layui-input">
            </div>
        </div>
        

        <div class="layui-form-item" style="padding-left: 1400px;">
            <div class="layui-input-block">
                <button type="submit" id="demo1" class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<script src="tinymce/tinymce.min.js"></script>
<script>
    var table=null;
    layui.use(['element', 'jquery', 'upload', 'form', 'table'], function () {
        var element = layui.element;
        var $ = layui.jquery;
        var upload = layui.upload;
        var form = layui.form;
        var layedit = layui.layedit;
       // var table=layui.table;


        // var uploadInst = upload.render({
        //     elem: '#test1'
        //     , url: 'http://localhost:8080/upload' //改成您自己的上传接口
        //     , before: function (obj) {
        //         //预读本地文件示例，不支持ie8
        //         obj.preview(function (index, file, result) {
        //             $('#demo1').attr('src', result); //图片链接（base64）
        //         });
        //     }
        //     , done: function (res) {
        //         //如果上传失败
        //         if (res.code > 0) {
        //             return layer.msg('上传失败');
        //         }
        //         //上传成功
        //         $("#img").val(res.location);
        //         layer.msg(res.msg);
        //     }
        //     , error: function () {
        //         //演示失败状态，并实现重传
        //         var demoText = $('#demoText');
        //         demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
        //         demoText.find('.demo-reload').on('click', function () {
        //             uploadInst.upload();
        //         });
        //     }
        // });

        // var subName = $("#subName").val();
        // console.log(subName)
        // var subMain = $("#mytextarea").val();
        // var answer = $("#answer")


        //监听提交
        form.on('submit(demo1)', function (data) {


            tinyMCE.editors["mytextarea"].save();
            data.field.subMain = $("#mytextarea").val();
            console.log(JSON.stringify(data.field));

            $.ajax({

                url: 'http://localhost:8080/addSubject',
                type: 'POST',
                dataType: 'JSON',
                data: data.field,
                success: function (result) {
                    if(result.code!=0){
                        alert("添加失败")
                    }else{
                        alert("添加成功")
                    }
                    $.get('subjectadd.html', function (result) {
                    $("#main").html(result);
                })       
                }
            });

            return false;
        });





        tinymce.init({
            selector: '#mytextarea',
            // menu:false,
            // menubar:false,
            plugins: 'image',
            toolbar: 'image|lineheight|newdocument|bold|italic|underline|strikethrough|alignleft|aligncenter|alignright|alignjustify|styleselect|formatselect|fontselect|fontsizeselect|cut|copy|paste|bullist|numlist|outdent|indent|blockquote|undo|redo|removeformat|subscript|superscript',
            images_upload_url: 'http://localhost:8080/upload',
            images_upload_base_path: 'http://localhost/',
        });

        form.render();

    });


</script>